//****************************即时通讯开始***********************************
var  stompClient = null;
//layim初始化  获取自己信息 好友列表 群信息
var Layim = null;

function layimInit(data, layim) {
    Layim = layim;
    //接入websocket
    connect();

    //面板外的操作
    var $ = layui.jquery, active = {
        chat: function () {
            //自定义会话
            layim.chat({
                name: 'test'
                , type: 'friend'
                , avatar: '//tva3.sinaimg.cn/crop.0.0.180.180.180/7f5f6861jw1e8qgp5bmzyj2050050aa8.jpg'
                , id: 5
            }).chat({
                name: 'admin'
                , type: 'friend'
                , avatar: '//tva3.sinaimg.cn/crop.0.0.180.180.180/7f5f6861jw1e8qgp5bmzyj2050050aa8.jpg'
                , id: 1
            })
        }
    };
    $('.site-demo-layim').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
        //关闭消息提示数
        $('.im .layui-badge').hide();
        msgNum = 0;
    });

    //监听发送消息
    layim.on('sendMessage', function (res) {
        var data = {
            username: res.mine.username,
            type: res.to.type,
            mId: res.mine.id,
            sId: res.to.id,
            content: res.mine.content
        }
        send(JSON.stringify(data));
    })
    layim.config({
        init: {
            //配置自己信息
            mine: {
                "username": data.userCount//我的昵称
                , "id": data.id //我的ID
                , "status": "online" //在线状态 online：在线、hide：隐身
                , "type": 'friend'
                , "avatar": data.userPhoto //我的头像
            }
        },
        brief: true //是否简约模式（若开启则不显示主面板）
        , initSkin: '2.jpg' //1-5 设置初始背景
        //,skin: ['aaa.jpg'] //新增皮肤
        , isfriend: true //是否开启好友
        , isgroup: true //是否开启群组
        , voice: false //声音提醒，默认开启，声音文件为：default.mp3
    });
}

//通讯连接
function connect() {
    let socket = new SockJS(defaultUrl+'/mywebsockets');
    stompClient = Stomp.over(socket);
    stompClient.connect({
        name: 'TEST' // 携带客户端信息
    }, function (frame) {
        subscribe();
    });
}

//订阅
var msgNum = 0;

function subscribe(frame) {
    var depth = stompClient.subscribe('/topic/news', function (respnose) {
        //判断当前聊天窗口是否显示
        if ($('.layui-layim-chat').length === 0 || $('.layui-layim-chat').css('display') === 'none') {
            msgNum += 1;
            $('.im .layui-badge').show().text(msgNum);
        }
        //接受消息并显示
        Layim.getMessage(JSON.parse(respnose.body));
    });
}
//  cpu订阅
function subscribe2(frame) {
    var res
    var depth = stompClient.subscribe('/topic/cpu', function (respnose) {
        res = JSON.parse(respnose.body)
        if( res != undefined){
            $(document).find('iframe').contents().find('iframe')[0].contentWindow.cpu(res)
        }  
    });
}
// 内存订阅
function subscribe3(frame) {
    var res
    var depth = stompClient.subscribe('/topic/memory', function (respnose) {
        res = JSON.parse(respnose.body)
        if( res != undefined){
            $(document).find('iframe').contents().find('iframe')[0].contentWindow.memory(res)
        }
    });
}
// 磁盘订阅1
function subscribe4(frame) {
    var res
    var depth = stompClient.subscribe('/topic/file', function (respnose) {
        res = JSON.parse(respnose.body)
        if( res != undefined){
            $(document).find('iframe').contents().find('iframe')[0].contentWindow.hardDisk1(res)
        } 
    });
    return res
}
// 磁盘订阅2
function subscribe5(frame) {
    var res
    var depth = stompClient.subscribe('/topic/file', function (respnose) {
        res = JSON.parse(respnose.body)
        if( res != undefined){
            $(document).find('iframe').contents().find('iframe')[0].contentWindow.hardDisk2(res)
        } 
    });
    return res
}
// 网络订阅
function subscribe6(frame) {
    var res
    var depth = stompClient.subscribe('/topic/ethernet', function (respnose) {
        res = JSON.parse(respnose.body)
        if( res != undefined){
            $(document).find('iframe').contents().find('iframe')[0].contentWindow.network(res)
        }
    });
}
// 进度展示
function subscribe7(frame) {
    var res
    var depth = stompClient.subscribe('/topic/progress', function (respnose) {
        res = JSON.parse(respnose.body)
        if( res != undefined){
            $(document).find('iframe').contents().find('iframe')[0].contentWindow.network(res)
        }
    });
}

//断开连接
function disconnect() {
    if (stompClient != null) {
        stompClient.disconnect();
    }
}

//发送消息
function send(data) {
    stompClient.send("/ts/testwebsocketstomp", {}, data);``
}

//点击组织结构节点触发事件
function memberChandle(obj) {
    //判断该节点是否是人员
    var data = obj.data;
    if (data.children && data.children.length > 0) return;

    $('.layim-member').hide();
    $('.layim-chat-list').show();

    //判断该人员是否已在聊天室中 存在则打开对话框
    if ($('.layim-friend' + data.id).length > 0) {
        $('.layim-friend' + data.id).click()
    } else {
        Layim.chat({
            name: data.title
            , type: 'friend'
            , avatar: '//tva3.sinaimg.cn/crop.0.0.180.180.180/7f5f6861jw1e8qgp5bmzyj2050050aa8.jpg'
            , id: data.id
        })
    }
}

//显示聊天室
function showMessage() {
    $('.layim-member').hide();
    $('.layim-chat-list').show();
}

//显示组织机构
function showMember() {
    $('.layim-member').show();
    $('.layim-chat-list').hide();
}

//添加好友
function addFriend() {

}


//****************************即时通讯结束***********************************
